import * as Blockly from 'blockly/core';

const generators ={};

const Generator = Blockly.Arduino ?? Blockly.Python;

generators.block_tool_xml_value = function () {
    let FIELD_NAME = this.getFieldValue("NAME");
    let STATEMENT_INPUT_CHILD = Generator.statementToCode(this, "CHILD") || "";
    let code = '';
    if (STATEMENT_INPUT_CHILD) {
        code = `<value name="${FIELD_NAME}">\n${STATEMENT_INPUT_CHILD}</value>\n`;
    } else {
        code = `<value name="${FIELD_NAME}"></value>\n`;
    }
    return code;
};

generators.block_tool_xml_statement = function () {
    let FIELD_NAME = this.getFieldValue("NAME");
    let STATEMENT_INPUT_CHILD = Generator.statementToCode(this, "CHILD") || "";
    let code = '';
    if (STATEMENT_INPUT_CHILD) {
        code = `<statement name="${FIELD_NAME}">\n${STATEMENT_INPUT_CHILD}</statement>\n`;
    } else {
        code = `<statement name="${FIELD_NAME}"></statement>\n`;
    }
    return code;
};

generators.block_tool_xml_block = function () {
    let FIELD_TYPE = this.getFieldValue("TYPE");
    if (!Blockly.Blocks[FIELD_TYPE]) {
        return '';
    }
    let STATEMENT_INPUT_CHILD = Generator.statementToCode(this, "CHILD") || "";
    let code = '';
    if (STATEMENT_INPUT_CHILD) {
        code = `<block type="${FIELD_TYPE}">\n${STATEMENT_INPUT_CHILD}</block>\n`;
    } else {
        code = `<block type="${FIELD_TYPE}"></block>\n`;
    }
    return code;
};

generators.block_tool_xml_shadow = function () {
    let FIELD_TYPE = this.getFieldValue("TYPE");
    if (!Blockly.Blocks[FIELD_TYPE]) {
        return '';
    }
    let STATEMENT_INPUT_CHILD = Generator.statementToCode(this, "CHILD") || "";
    let code = '';
    if (STATEMENT_INPUT_CHILD) {
        code = `<shadow type="${FIELD_TYPE}">\n${STATEMENT_INPUT_CHILD}</shadow>\n`;
    } else {
        code = `<shadow type="${FIELD_TYPE}"></shadow>\n`;
    }
    return code;
};

generators.block_tool_xml_field = function () {
    let FIELD_NAME = this.getFieldValue("NAME");
    let FIELD_VALUE = this.getFieldValue("VALUE");
    let code = '';
    code = `<field name="${FIELD_NAME}">${FIELD_VALUE}</field>\n`;
    return code;
};

export default generators;